package com.gis.postgis.geojson.parsers;

import java.io.IOException;
import java.lang.annotation.Annotation;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gis.postgis.geojson.annotation.GeoJsonGeometry;
import com.gis.postgis.geojson.annotation.GeoJsonId;
import com.gis.postgis.geojson.annotation.GeoJsonType;
import com.gis.postgis.geojson.deserializer.GeoJsonDeserializer;
import com.gis.postgis.geojson.document.DocumentFactoryException;
import com.gis.postgis.geojson.introspection.Annotated;
import com.gis.postgis.geojson.introspection.IntrospectionDocumentFactory;
import com.google.common.collect.ListMultimap;

import static com.gis.postgis.geojson.GeoJson.GEOMETRY;
import static com.gis.postgis.geojson.GeoJson.ID;
import static com.gis.postgis.geojson.GeoJson.PROPERTIES;
import static com.gis.postgis.geojson.GeoJson.TYPE;

/**
 * 〈功能简述〉<br>
 * 〈〉
 *
 * @author Mr.QL
 * @ClassName FeatureGeoJsonParser
 * @Date 2022-03-12 22:38
 * @Version 1.0
 */
public class FeatureGeoJsonParser<T> extends BaseIgnoreDeserializerObjectMapper<T> implements GeoJsonBaseParser<T> {

    public FeatureGeoJsonParser(JavaType valueType, Class<? extends GeoJsonDeserializer> deserializeClazz) {
        super(valueType, deserializeClazz);
    }

    @Override
    public T deserialize(JsonParser jsonParser) throws IOException {
        ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec();
        //ObjectCodec oc = jsonParser.getCodec();
        ObjectNode nodeObject = (ObjectNode) mapper.readTree(jsonParser);
        GeoJsonType geoJsonType = valueType.getRawClass().getAnnotation(GeoJsonType.class);
        String featureName = geoJsonType.type().getName();

        //获取对象空间数据字段反序列化名称
        String geometryFieldName = null;
        String idFieldName = null;
        try {
            ListMultimap<Class<? extends Annotation>, Annotated> index = IntrospectionDocumentFactory.index(valueType.getRawClass());
            Annotated geometryAnnotated = IntrospectionDocumentFactory.oneOrNull(index, GeoJsonGeometry.class);
            Annotated idAnnotated = IntrospectionDocumentFactory.oneOrNull(index, GeoJsonId.class);
            if (null != geometryAnnotated) {
                geometryFieldName = geometryAnnotated.getName();
            }
            if (null != idAnnotated) {
                idFieldName = idAnnotated.getName();
            }
        } catch (DocumentFactoryException e) {
            e.printStackTrace();
        }
        //获取数据
        JsonNode id = nodeObject.get(ID);
        JsonNode geometry = nodeObject.get(GEOMETRY);
        ObjectNode properties = (ObjectNode) nodeObject.get(PROPERTIES);
        //组装
        nodeObject.remove(ID);
        nodeObject.remove(TYPE);
        nodeObject.remove(GEOMETRY);
        nodeObject.remove(PROPERTIES);
        if (null != idFieldName && null != id) {
            nodeObject.replace(idFieldName, id);
        }
        if (null != geometryFieldName && null != geometry) {
            nodeObject.replace(geometryFieldName, geometry);
        }
        if (null != properties) {
            nodeObject.setAll(properties);
        }
//    JsonParser parser = copyMapper.treeAsTokens(nodeObject);
//    T t = copyMapper.readValue(parser, valueType);
        ObjectMapper copyMapper = getIgnoreDeserializerAnnotationMapper(mapper);
        T t = copyMapper.convertValue(nodeObject, valueType);
        return t;
    }
}
